﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using EHTSaleManager.Code.BLL;
using EHTSaleManager.Code.Entity;
using EHTSaleManager.Code.Utility;
using System.IO;

namespace EHTSaleManager.UIAdmin
{
    public partial class AdminDocument : System.Web.UI.Page
    {
        public Int32 DocumentID = -1;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Request.QueryString["DocumentID"] != null)
                {
                    try
                    {
                        DocumentID = Int32.Parse(Request.QueryString["DocumentID"].ToString());
                    }
                    catch (Exception ex)
                    {
                        DocumentID = -1;
                    }
                }

                if (DocumentID == -1)
                {
                    ViewState["action"] = HangSo.ACTION_ADD;

                }
                else
                {
                    Document theDocument = DocumentProvider.GetDocumentByDocumentID(DocumentID);

                    if (theDocument == null)
                        Response.Redirect("AdminDocuments.aspx");
                    else
                    {
                        this.txtDescription.Text = theDocument.Description;
                        ViewState["ID"] = theDocument.DocumentID;
                        ViewState["DocumentUrl"] = theDocument.DocumentUrl;
                        ViewState["action"] = HangSo.ACTION_UPDATE;
                    }
                }
            }
        }

        private Boolean ValidateData()
        {
            String message = "";
            Boolean isSuccess = true;
            string sExtension = ".doc,.docx,.pdf,.xlsx,.xls,.txt,.DOC,.DOCX,.XLS,.XLSX,.TXT,.PDF";

            try
            {
                if (ViewState["action"].ToString() == HangSo.ACTION_ADD)
                {
                    if (!this.fuDocumentUrl.HasFile)
                    {
                        isSuccess = false;
                        message = "Vennligst bla fil dokument.";
                    }
                    else
                    {
                        if (sExtension.IndexOf(Path.GetExtension(fuDocumentUrl.PostedFile.FileName)) < 0)
                        {
                            isSuccess = false;
                            message = "Last opp dokument bare.";
                        }

                    }
                }
                else
                {
                    if (this.fuDocumentUrl.HasFile && (sExtension.IndexOf(Path.GetExtension(fuDocumentUrl.PostedFile.FileName)) < 0))
                    {
                        isSuccess = false;
                        message = "Last opp dokument bare.";
                    }
                }

            }
            catch (ApplicationException ex)
            {
                throw new ApplicationException(ex.Message);
            }

            if (!isSuccess)
                ltrMessage.Text = message;

            return isSuccess;
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            String action = ViewState["action"].ToString();

            if (!ValidateData())
                return;

            Page.Validate();

            if (Page.IsValid)
            {
                Document theDocument = new Document();
                theDocument.Description = this.txtDescription.Text.Trim();

                string newFileName = "";

                if (this.fuDocumentUrl.HasFile)
                {
                    String fileName = fuDocumentUrl.PostedFile.FileName;
                    String fileType = Path.GetExtension(fileName);
                    newFileName = DateTime.Now.Ticks.ToString() + fileType;
                    string str = Server.MapPath("~/");
                    str += "ImageDatas\\Documents\\";
                    DirectoryInfo directory = new DirectoryInfo(str);

                    if (!directory.Exists)
                    {
                        DirectoryInfo drt = new DirectoryInfo("\\ImageDatas");
                        if (!drt.Exists)
                        {
                            directory = new DirectoryInfo(Server.MapPath("~/"));

                            directory = directory.CreateSubdirectory("ImageDatas");

                        }
                        directory = new DirectoryInfo(Server.MapPath("~/").ToString() + "\\ImageDatas");
                        directory = directory.CreateSubdirectory("Documents");
                    }
                    fuDocumentUrl.PostedFile.SaveAs(Server.MapPath("~/") + "ImageDatas//Documents//" + newFileName);

                    theDocument.DocumentUrl = newFileName.Trim();
                }
                else
                {
                    theDocument.DocumentUrl = ViewState["DocumentUrl"].ToString();
                }

                if (action == HangSo.ACTION_ADD)
                {
                    theDocument.DocumentID = -1;
                    DocumentProvider.UIDocuments(theDocument, true);
                }
                else
                {
                    theDocument.DocumentID = Int32.Parse(ViewState["ID"].ToString());
                    DocumentProvider.UIDocuments(theDocument, false);
                }

                Response.Redirect("AdminDocuments.aspx");
            }
        }
        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("AdminDocuments.aspx");
        }
    }

}